import React from 'react';
import { View, Image, Linking } from 'react-native';
import SplashScreen from "react-native-splash-screen"
import { Lt, Color, Label, Dialog, Jpp, Jx, Images, Navigator, Button, Nav, Storage, Touch, cSys } from "../_utils";
import { Net, NET_RI, NET_NR, NET_ND, NET_NT } from "../_net"
import { cTemp } from '../_util';
import { ViewProtocol } from '../ViewProtocol';
import { Overlay } from '../_teaset';
import { cApply } from './_apply';

function toMainWithRole(roleId){
    cTemp.setRoleId(roleId);
    Navigator.reset("ApplyMain");
}

export function toMainWithToken(token, phone){
    if(token.userId && token.token){
        token = token.userId + "_" + token.token;
        Net._token = token;
        console.log("Net._token = ", token);
        Storage.save(Storage.SESSION, token);
        if(phone){
            Storage.save(Storage.ACCOUNT, phone);
        }
        Net.request("team/myRole", null, {NET_RI}).then(r=>{
            toMainWithRole(r.data);
        });
        return true;
    }
    console.log("token数据: ", Jx.stringify(token));
    return false;
}

export class ApplyAutoLogin extends React.PureComponent{
    constructor(props){
        super(props);

        // const { obj } = this.props.navigation.state.params;
        // this.obj = obj;
        this.account = "";
        this.switchUrl = 0;

        this.onProtocol =  this.onProtocol.bind(this);
    }
    componentWillMount(){
        Storage.promiseLoad(Storage.ACCOUNT).then(r=>{
            this.account = r;
        })
    }
    componentDidMount(){
        setTimeout(SplashScreen.hide.bind(SplashScreen), 1)
    }

    static navigationOptions = {
        header : null,
    }

    onVisit(visit){
        cApply.setVisitor(visit);
        if(visit){
            Net.request("loginUser", {phone:13798529601, validate:131072}, {NET_RI, NET_NT}).then(uData=>{
                console.warn(Jx.stringify(uData));
                toMainWithToken(uData.data, 13798529601);
            });
        }else{
            Navigator.push("ApplyLogin", {account : this.account});
        }

    }

    onProtocol(){
        Overlay.hide(this.readRef);
        this.readRef = Overlay.show(<ViewProtocol
            onClose={()=>{Overlay.hide(this.readRef)}}
            onRead={()=>{
                Overlay.hide(this.readRef)
            }}
        />);
    }

    render(){
        return <View style = {{flex:1, justifyContent:"space-between", alignItems:"center"}}>
            <Nav.StatusBar/>
            <View style = {{height:"60%", justifyContent:"center", alignItems:"center"}}>
                <Touch
                    onPress={()=>++this.switchUrl} 
                    onLongPress={()=>{
                        if(this.switchUrl > 3){
                            Net.switchUrl();
                        }
                    }}
                >
                    <Image source = {Images.logo} />
                </Touch>
                <Label style = {{fontSize:Lt.c20, color:Color.c3, marginVertical:Lt.margin}}>{Jpp.gameName}</Label>
                <Label style = {{color:Color.c3}}>省钱神器</Label>
            </View>
            <View style = {{height:"40%", justifyContent:"center", alignItems:"center"}}>
                <Button.FlexPure 
                    style = {{marginBottom:20}} 
                    title = { "手机登录" } 
                    onPress = {this.onVisit.bind(this, false)}
                />
                <Button.Round style = {{
                    color:Color.c6,
                    fontSize:Lt.c16,
                }}
                    title = {"游客登录"}
                    onPress = {this.onVisit.bind(this, true)}
                />
                <View style={{marginTop:Lt.margin, flexDirection:"row", alignItems:"center", alignSelf:"center"}}>
                    <Touch style={{padding:Lt.pad}} onPress={this.onProtocol}>
                        <View style={{flexDirection:"row", alignItems:'center'}}>
                            <Image source={Images.yuandu_gou}/>
                            <Label style = {{color:Color.c9}}> 我已阅读用户协议</Label>
                        </View>
                    </Touch>
                </View>
            </View>
        </View>;
    }
}